<template>
  <div class="release">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

      <el-form-item label="类型">
        <el-radio label="1" v-model="radio">闲置贴</el-radio>
        <el-radio label="2" v-model="radio">校园贴</el-radio>
      </el-form-item>

      <el-form-item label="标题" prop="Title">
        <el-input v-model="ruleForm.Title"></el-input>
      </el-form-item>

      <el-form-item label="内容" prop="contant">
        <el-input v-model="ruleForm.contant"></el-input>
      </el-form-item>

      <el-form-item label="校区" prop="Address">
        <el-radio-group v-model="ruleForm.Address">
          <el-radio label="2">社湾校区</el-radio>
          <el-radio label="1">官塘校区</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="价格" prop="price" v-show="radio == '1' ? true : false">
        <el-input v-model.number="ruleForm.price"></el-input>
      </el-form-item>

      <el-form-item label="图片">

        <el-upload action="ruleForm.file" list-type="picture-card" :auto-upload="false">
          <i slot="default" class="el-icon-plus"></i>
          <div slot-scope="{file}">
            <img class="el-upload-list__item-thumbnail" :src="ruleForm.file = file.url" alt="">
            <span class="el-upload-list__item-actions">
              <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)">
                <i class="el-icon-delete"></i>
              </span>
            </span>
          </div>
        </el-upload>

      </el-form-item>

      <el-form-item v-show="radio == '1' ? true : false">
        <el-button type="primary" @click="submitForm1(!$store.state.isLogin)">立即发布1</el-button>
        <el-button @click="resetForm('ruleForm', file)">重置</el-button>
      </el-form-item>
      <el-form-item v-show="radio == '2' ? true : false">
        <el-button type="primary" @click="submitForm2(!$store.state.isLogin)">立即发布2</el-button>
        <el-button @click="resetForm('ruleForm', file)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import axios from "axios";
export default {
  name: 'ReleaseView',

  data() {
    return {
      radio: '1',
      disabled: false,
      ruleForm: {
        Address: '',
        Title: '',
        contant: '',
        file: '',
      },


      rules: {
        Title: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          { min: 10, max: 30, message: '长度在 10 到 30 个字符', trigger: 'blur' }
        ],
        Address: [
          { required: true, message: '请选择校区', trigger: 'change' }
        ],
        contant: [
          { required: true, message: '请输入内容', trigger: 'blur' },
        ],
        file: [
          { required: true, message: '请上传图片', trigger: 'blur' },
        ],
      }
    };
  },

  methods: {
    submitForm2(islogin) {
      //判断是否登录
      if (!islogin) {
             let addShare = new URLSearchParams();
              addShare.append("school", this.ruleForm.Address);
              addShare.append("title", this.ruleForm.Title);
              addShare.append("contant", this.ruleForm.contant);
              addShare.append("file", this.ruleForm.file);

        axios.get('/api/CampusSharing/CampusSharingXiaoQu?Address=0'
          ,
          {
            params:{
              data:addShare
            }
             
           
          }
        )
          .then(res => {
            console.log(res);
          }).catch(err => {
            console.log(err);
          })
      }
      else {
        alert("请登录");
        this.$router.push('/login');
      }

    },
    submitForm1(islogin) {
      if (!islogin) {
        axios.get('/api/Commoditys/GetCommoditys')
          .then(res => {
            console.log(res)
          }).catch(err => {
            console.log(err);
          })
      }
      else {
        alert("请登录");
        this.$router.push('/login');
      }
    },

    resetForm(ruleForm, file) {
      console.log(file);
      this.$refs[ruleForm].resetFields();

    },
    //删除图片
    handleRemove(file) {
      console.log(file);
      file.url = '';
    },


  }
}


</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>